<?php
// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/ci_types/tcms_ci_normal_ci_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_type_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpt_ci_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpt/vpage_abs_dbc.class.php');


class VPage_Product_DBC extends VPage_Abs_DBC {
	private $title = '';
	private $content = '';
	private $intro = '';
	private $meta_description = '';

	function __construct() {

	}


	public function save(DB_Connection $dbc, $current_user_id = 0) {
		$ret = false;

		if ($this->get_id()) {
			$vpage = new TCMS_VPage_DBC();
			if ($vpage->load_by_id($dbc, $this->get_id())) {

				$this->save_system_attribute($dbc, $vpage->get_id(), $vpage->get_template(), 'title', $this->get_title());
				$this->save_system_attribute($dbc, $vpage->get_id(), $vpage->get_template(), 'meta_description', $this->get_meta_description());


				// Saving content
				$vpage_ci = $this->load_content_item($dbc, $this->get_vpage_id(), 'content');
				if (!$vpage_ci) {
					$vpt_ci = new TCMS_VPT_CI_DBC();
					if ($vpt_ci->load_by_vpage_and_hid($dbc, $this->get_vpage_id(), 'content')) {
						$ci_normal_ci = new TCMS_CI_Normal_CI_DBC();
						$ci_normal_ci->get_content()->set_value($this->get_content());
						if ($ci_normal_ci->save($dbc, $current_user_id)) {
							$vpage_ci = new TCMS_VPage_CI_DBC();
							$vpage_ci->set_t_ci_id($vpt_ci->get_id());
							$vpage_ci->set_type($ci_normal_ci->get_ci()->get_type());
							$vpage_ci->set_vpage($this->get_vpage_id());
							$vpage_ci->set_ci_id($ci_normal_ci->get_id());
							$vpage_ci->save($dbc);
						}
					}
				} else {
					$ci_normal_ci = new TCMS_CI_Normal_CI_DBC();
					if ($ci_normal_ci->load_by_id($dbc, $vpage_ci->get_ci_id())) {
						$ci_normal_ci->get_content()->set_value($this->get_content());
						$ci_normal_ci->save($dbc, $current_user_id);
					}
				}


				// Saving intro
				$vpage_ci = $this->load_content_item($dbc, $this->get_vpage_id(), 'intro');
				if (!$vpage_ci) {
					$vpt_ci = new TCMS_VPT_CI_DBC();
					if ($vpt_ci->load_by_vpage_and_hid($dbc, $this->get_vpage_id(), 'intro')) {
						$ci_normal_ci = new TCMS_CI_Normal_CI_DBC();
						$ci_normal_ci->get_content()->set_value($this->get_intro());
						if ($ci_normal_ci->save($dbc, $current_user_id)) {
							$vpage_ci = new TCMS_VPage_CI_DBC();
							$vpage_ci->set_t_ci_id($vpt_ci->get_id());
							$vpage_ci->set_type($ci_normal_ci->get_ci()->get_type());
							$vpage_ci->set_vpage($this->get_vpage_id());
							$vpage_ci->set_ci_id($ci_normal_ci->get_id());
							$vpage_ci->save($dbc);
						}
					}
				} else {
					$ci_normal_ci = new TCMS_CI_Normal_CI_DBC();
					if ($ci_normal_ci->load_by_id($dbc, $vpage_ci->get_ci_id())) {
						$ci_normal_ci->get_content()->set_value($this->get_intro());
						$ci_normal_ci->save($dbc, $current_user_id);
					}
				}

				TCMS_VPage_DBC::update_last_modified($dbc, $vpage->get_id(), $current_user_id);

				$ret = true;
			}
		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$title = $this->load_system_attribute($dbc, $id, 'title');
		if ($title) {
			$this->set_title($title->get_value());
		}

		$meta_description = $this->load_system_attribute($dbc, $id, 'meta_description');
		if ($meta_description) {
			$this->set_meta_description($meta_description->get_value());
		}

		$content_ci = $this->load_content_item($dbc, $id, 'content');
		if ($content_ci) {
			$ci_normal_ci = new TCMS_CI_Normal_CI_DBC();
			if ($ci_normal_ci->load_by_id($dbc, $content_ci->get_ci_id())) {
				$this->set_content($ci_normal_ci->get_content()->get_value());
			}
		}

		$intro_ci = $this->load_content_item($dbc, $id, 'intro');
		if ($intro_ci) {
			$ci_normal_ci = new TCMS_CI_Normal_CI_DBC();
			if ($ci_normal_ci->load_by_id($dbc, $intro_ci->get_ci_id())) {
				$this->set_intro($ci_normal_ci->get_content()->get_value());
			}
		}		
		
		$this->set_vpage_id($id);

		return $id;
	}


	public function set_title($title) {
		$this->title = $title;
	}


	public function get_title() {
		return $this->title;
	}


	public function set_content($content) {
		$this->content = $content;
	}


	public function get_content() {
		return $this->content;
	}


	public function set_intro($intro) {
		$this->intro = $intro;
	}


	public function get_intro() {
		return $this->intro;
	}


	public function set_meta_description($meta_description) {
		$this->meta_description = $meta_description;
	}


	public function get_meta_description() {
		return $this->meta_description;
	}
}